<template>
  <el-card class="form-container" shadow="never">
    <el-form :model="menu"
             :rules="rules"
             ref="menuFrom"
             label-width="170px">
      <el-form-item label="菜单名称：" prop="rule_name">
        <el-input v-model="menu.rule_name"></el-input>
      </el-form-item>
      <el-form-item label="菜单栏标识符：" prop="rule_label">
        <el-input v-model="menu.rule_label"></el-input>
      </el-form-item>
      <el-form-item label="菜单栏所属父级id：" prop="rule_parentid">
        <el-select @change="changeId()" v-model="menu.rule_parentid" placeholder="请选择">
          <el-option
            label="无上级菜单"
            value="0">
          </el-option>
          <el-option
            v-for="item in list"
            :key="item.id"
            :label="item.rule_name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="菜单栏模块名称：" prop="rule_app">
        <el-input v-model="menu.rule_app"></el-input>
      </el-form-item>
      <el-form-item label="菜单栏控制器名称：" prop="rule_controller">
        <el-input v-model="menu.rule_controller"></el-input>
      </el-form-item>
      <el-form-item label="菜单栏方法名称：" prop="rule_action">
        <el-input v-model="menu.rule_action"></el-input>
      </el-form-item>
      <el-form-item label="菜单栏路由跳转链接：" prop="rule_redirect">
        <el-input v-model="menu.rule_redirect"></el-input>
      </el-form-item>
      <el-form-item label="是否显示：">
        <el-switch
          :active-value="1"
          :inactive-value="0"
          v-model="menu.rule_isshow">
        </el-switch>
        <!--    <el-switch v-model="menu.rule_isshow"></el-switch>-->
      </el-form-item>
      <el-form-item label="前端图标：" prop="rule_iconImg">
        <el-input v-model="menu.rule_iconImg"></el-input>
      </el-form-item>
      <el-form-item label="排序：" prop="rule_orderid">
        <el-input v-enter-number v-model="menu.rule_orderid"></el-input>
      </el-form-item>
      <!--<el-form-item label="上级菜单：">
        <el-select v-model="menu.parentId"
                   placeholder="请选择菜单">
          <el-option
            v-for="item in selectMenuList"
            :key="item.id"
            :label="item.title"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>-->
      <!--  <el-form-item label="前端名称：" prop="name">
          <el-input v-model="menu.name"></el-input>
        </el-form-item>
        <el-form-item label="前端图标：" prop="icon">
          <el-input v-model="menu.icon" style="width: 80%"></el-input>
          <svg-icon style="margin-left: 8px" :icon-class="menu.icon"></svg-icon>
        </el-form-item>
        <el-form-item label="是否显示：">
          <el-radio-group v-model="menu.hidden">
            <el-radio :label="0">是</el-radio>
            <el-radio :label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序：">
          <el-input v-model="menu.sort"></el-input>
        </el-form-item>-->
      <el-form-item>
        <el-button type="primary" @click="onSubmit('menuFrom')">提交</el-button>
        <el-button v-if="!isEdit" @click="resetForm('menuFrom')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import {menuAdd, menuLine, menuEdit, menuList} from '@/api/menu';

const defaultMenu = {
  rule_name: '',
  rule_label: '',
  rule_parentid: '',
  rule_app: '',
  rule_controller: '',
  rule_action: '',
  rule_redirect: '',
  rule_iconImg: '',
  rule_orderid: '',
  rule_isshow: false,

};
export default {
  name: "MenuDetail",
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      list: [],
      menu: Object.assign({}, defaultMenu),
      selectMenuList: [],
      rules: {
        rule_name: [
          {required: true, message: '请输入菜单名称', trigger: 'blur'},
          {min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur'}
        ],
        rule_label: [
          {required: true, message: '菜单栏标识符', trigger: 'blur'},
          {min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur'}
        ],
        rule_parentid: [
          {required: true, message: '菜单栏所属父级id', trigger: 'blur'}
        ],
        rule_app: [
          {required: true, message: '菜单栏模块名称', trigger: 'blur'}
        ],
        rule_controller: [
          {required: true, message: '菜单栏控制器名称', trigger: 'blur'}
        ],
        rule_action: [
          {required: true, message: '菜单栏方法名称', trigger: 'blur'}
        ],
        rule_redirect: [
          {required: true, message: '菜单栏路由跳转链接', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入前端名称', trigger: 'blur'},
          {min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur'}
        ],
        icon: [
          {required: true, message: '请输入前端图标', trigger: 'blur'},
          {min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    if ( this.isEdit ) {
      menuLine(this.$route.query.id).then(response => {
        this.menu = response.data;
      });
    } else {
      this.menu = Object.assign({}, defaultMenu);
    }
    /*this.getSelectMenuList();*/
    this.getList()
  },
  methods: {
    changeId(value){
      console.log(this.menu.rule_parentid);
    },
    /**
     * 菜单列表数据
     */
    getList() {
      this.listLoading = true;
      menuList().then(response => {
        this.listLoading = false;
        console.log(response);
        this.list = response.data.list;
      });
    },
    getSelectMenuList() {
      /* fetchList(0, {pageSize: 100, pageNum: 1}).then(response => {
         this.selectMenuList = response.data.list;
         this.selectMenuList.unshift({id: 0, title: '无上级菜单'});
       });*/
    },
    onSubmit(formName) {
      this.$refs[ formName ].validate((valid) => {
        if ( valid ) {
          this.$confirm('是否提交数据', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            if ( this.isEdit ) {
              menuEdit(Object.assign({id: this.$route.query.id}, this.menu)).then(response => {
                this.$message({
                  message: '修改成功',
                  type: 'success',
                  duration: 1000
                });
                this.$router.back();
              });
            } else {
              menuAdd(this.menu).then(response => {
                this.$refs[ formName ].resetFields();
                this.resetForm(formName);
                this.$message({
                  message: '提交成功',
                  type: 'success',
                  duration: 1000
                });
                this.$router.push({path: '/ums/menu'})
              });
            }
          });

        } else {
          this.$message({
            message: '验证失败',
            type: 'error',
            duration: 1000
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[ formName ].resetFields();
      this.menu = Object.assign({}, defaultMenu);
      /*
         this.getSelectMenuList();*/
    },
  }
}
</script>

<style scoped>

</style>
